import { getSetting } from '@/api/setting.js'
import setTitle from '@/utils/setTitle.js'

export default {
    namespaced: true,
    state: {
        loading: false,
        data: null
    },
    mutations: {
        setLoading(state, payload) {
            state.loading = payload;
        },
        setSetting(state, payload) {
            state.data = payload;
        }
    },
    actions: {
        async fetchData(ctx) {
            ctx.commit('setLoading', true);
            var resp = await getSetting();
            ctx.commit('setSetting', resp);
            ctx.commit('setLoading', false);
            if (resp.favicon) {
                let link = document.querySelector("link[ref='shortcut icon']");
                if (link) {
                    return;
                }
                link = document.createElement("link");
                link.rel = "shortcut icon";
                link.type = "images/x-icon";
                link.href = resp.favicon;
                document.querySelector("head").appendChild(link);
            }
            if (resp.siteTitle) {
                setTitle.setSiteTitle(resp.siteTitle)
            }
        },
    }

}